#include "drv_tmr.h"
#include "drv_vbus_sw.h"
#include "flexible_button.h"

__IO uint16_t task_disp_tick;

// 100Hz
void tick_tmr_config(void)
{
    crm_periph_clock_enable(CRM_TMR6_PERIPH_CLOCK, TRUE);
    
    tmr_base_init(TMR6, 18749, 63);
    tmr_cnt_dir_set(TMR6, TMR_COUNT_UP);
    tmr_period_buffer_enable(TMR6, FALSE);

    nvic_irq_enable(TMR6_GLOBAL_IRQn, 0, 0);
    
    tmr_flag_clear(TMR6, TMR_OVF_FLAG);
    tmr_interrupt_enable(TMR6, TMR_OVF_INT, TRUE);
    tmr_counter_enable(TMR6, TRUE);
}

void TMR6_GLOBAL_IRQHandler(void)
{
    if(tmr_flag_get(TMR6, TMR_OVF_FLAG) != RESET)
    {
        task_disp_tick++;
        vbus_ovc_scan(); // ovc scan
        flex_button_scan(); // button scan
        tmr_flag_clear(TMR6, TMR_OVF_FLAG);
    }
}